package com.jluzh10.learning.service.Impl;

import com.jluzh10.learning.dao.CommentParameMapper;
import com.jluzh10.learning.domain.CommentParame;
import com.jluzh10.learning.service.CommentParameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;

@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class CommentParameServiceImpl implements CommentParameService {

    @Autowired
    private CommentParameMapper commentParameMapper;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public CommentParame findCommentParame(int courseId) {
        CommentParame commentParame=new CommentParame();
        double sumScope=commentParameMapper.sumScope(courseId);
        double five=commentParameMapper.avgFiveStart(courseId);
        double four=commentParameMapper.avgFouStart(courseId);
        double three=commentParameMapper.avgThreeStart(courseId);
        double two=commentParameMapper.avgTwoStart(courseId);
        double one=commentParameMapper.avgOneStart(courseId);

        commentParame.setFiveStart(Double.parseDouble(new DecimalFormat("#.##").format((five/sumScope)*100)));
        commentParame.setFouStart( Double.parseDouble(new DecimalFormat("#.##").format((four/sumScope)*100)));
        commentParame.setThreeStart( Double.parseDouble(new DecimalFormat("#.##").format((three/sumScope)*100)));
        commentParame.setTwoStart(Double.parseDouble(new DecimalFormat("#.##").format((two/sumScope)*100)));
        commentParame.setOneStart(Double.parseDouble(new DecimalFormat("#.##").format((one/sumScope)*100)));
        return commentParame;
    }
}
